<template>
  <el-dialog title="新增部门" :visible="isShowDialog" @close="btnCancel">
    <el-form label-width="80px">
      <el-form-item label="经纪">
        <el-select v-model="formData.agent">
          <el-option
            v-for="item in agents"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="formData.category">
          <el-option
            v-for="item in categories"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="标签">
        <!-- 遍历渲染 tags -->
        <el-tag
          v-for="tag in formData.tags"
          :key="tag"
          closable
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <!-- 输入框和按钮相互切换 -->
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="formData.address" />
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="formData.price" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="formData.desc" />
      </el-form-item>
      <el-form-item label="内容">
        <VueEditor v-model="formData.content" />
      </el-form-item>
      <el-form-item label="介绍图片 ">
        <ImageUpload ref="photo" :limit="3" />
      </el-form-item>
      <el-form-item label="封面">
        <!-- <el-input v-model="formData.image" /> -->
        <ImageUpload ref="image" :limit="1" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button size="small" type="primary" @click="btnOK">确定</el-button>
      <el-button size="small" @click="btnCancel">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getAgentList } from '@/api/agent'
import { getCateList } from '@/api/category'
import { addProperty, editProperty } from '@/api/properties'
import { VueEditor } from 'vue2-editor'
export default {
  components: {
    VueEditor
  },
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        'title': '', // 标题
        'desc': '', // 描述
        'image': '',
        'content': '', // 内容
        'address': '', // 地址
        'price': '', // 价格
        'photos': [], // 图片, url 组成数组
        'tags': [], // 标签, 字符串组成数组
        'category': '' // 属于哪个分类 id 值
      },
      categories: [],
      agents: [],

      // 标签是否显示输入框(否则是按钮)
      inputVisible: false,
      // 输入框绑定字符串
      inputValue: ''
    }
  },
  created() {
    this.getCateList()
    this.getAgentList()
  },
  methods: {
    // 点击关闭(删除)
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },
    // 确认添加
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    // 点击按钮显示输入框
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    async getCateList() {
      this.categories = await getCateList()
      console.log('分类列表', this.categories)
    },
    async getAgentList() {
      this.agents = await getAgentList()
      console.log('经纪列表', this.agents)
    },
    async btnOK() {
      // 判断封面是否存在
      if (!this.$refs.image.fileList[0]) {
        this.$message.error('清先上传封面')
        return
      }
      const data = {
        ...this.formData,
        image: this.$refs.image.fileList[0].url,
        photos: this.$refs.photo.fileList.map(item => item.url)
      }

      // 发请求
      if (this.formData.id) {
        // 编辑
        await editProperty(data)
      } else {
        // 新增
        await addProperty(data)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 通知父组件进行各种操作
      this.$parent.isShowDialog = false
      this.$parent.loadPage()
    },
    btnCancel() {
      this.formData = {
        'title': '', // 标题
        'desc': '', // 描述
        'image': '',
        'content': '', // 内容
        'address': '', // 地址
        'price': '', // 价格
        'photos': [], // 图片, url 组成数组
        'tags': [], // 标签, 字符串组成数组
        'category': '' // 属于哪个分类 id 值
      }

      // 图片上传组件也需要清理
      this.$refs.photo.fileList = []
      this.$refs.image.fileList = []

      this.$parent.isShowDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
 .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

  ::v-deep .ql-toolbar.ql-snow {
    line-height: 1;
  }
</style>
